import { defineStore } from "pinia"
import axios from 'axios'
import { handleError } from '@/plugins/err-handle'
import { sortBy } from 'lodash'

export const useDocumentStore = defineStore("document", {
  state: () => ({
    currentDoc: {
      "title": '',
      "termodel": null,
      "status": '1',
      "detail": [{
        "title": "",
        "content": "",
        "images": [],
        "sort": 1,
        "status": 1,
      }],
    },
    detailTemp: {
      "title": "",
      "content": "",
      "images": [],
      "sort": null,
      "status": 1,
    },
    documents: [],
    total_count: 0,
    
  }),
  getters: {
    // doubleCount: (state) => state.counter + 2,
  },
  actions: {
    resetCurrentDoc() {
      this.currentDoc = {
        "id" : null,
        "title": '',
        "termodel": null,
        "status": '1',
        "detail": [{
          "title": '',
          "content": '',
          "images": [],
          "sort": null,
          "status": 1,
        }],
      }
    },
    // 列表
    fetchItems(params) {
      return new Promise((resolve, reject) => {
        axios.get('/admin/document', { params })
          .then((response) => {
            this.documents = response.data.data
            this.total_count = response.data.meta.total_count

            resolve(response)
          })
          .catch((err) => {
            handleError(err)
            reject(err)
          })
        })
    },
    // 单文件
    fetchItem(id) {
      return new Promise((resolve, reject) => {
        axios
          .get(`/admin/document/${id}`)
          .then((response) => {
            console.log('admin document one:', response.data)
            if (response.data) {
              response.data.status = String(response.data.status)
              this.currentDoc = this.dealDetail(response.data)
            }
            resolve(response)
          })
          .catch((err) => {
            handleError(err)
            reject(err)
          })
      })
    },

    // 添加
    addItem(data) {
      return new Promise((resolve, reject) => {

        axios.post('/admin/document', data)
          .then((response) => {
            console.log('document addItem: ', response) 
            response.data.status = String(response.data.status)
            this.currentDoc = this.dealDetail(response.data)

            resolve(response)
          })
          .catch((err) => {
            handleError(err)
            reject(err)
          })
        })
    },

    // 编辑
    updateItem(data) {
      return new Promise((resolve, reject) => {
        axios
          .put(`/admin/document/${data.id}`, data)
          .then((response) => {
            console.log('编辑成功', response)
            if (response.data) {
              console.log('document updateItem: ', response) // users
              response.data.status = String(response.data.status)
              this.currentDoc = this.dealDetail(response.data)
            }

            resolve(response)
          })
          .catch((err) => {
            console.log('编辑错误')
            handleError(err)
            reject(err)
          })
      })
    },

    // 删除
    deleteItem(ids) {
      return new Promise((resolve, reject) => {
        axios
          .post('/admin/document/delete', ids)
          .then((response) => {
            resolve(response)
          })
          .catch((err) => {
            handleError(err)
            reject(err)
          })
      })
    },

    // 处理 detail
    dealDetail(data) {
      // detail
      data.detail = sortBy(data.detail, ['sort'])
      data.detail.forEach(element => {
        element.images = Array.isArray(element.images) ? element.images : JSON.parse(element.images)
      })
      return data
    },

    // 获取pdf文件
    getPdf(id) {
      return new Promise((resolve, reject) => {
        axios
          .post('/admin/attachment/show-pdf', {id})
          .then((response) => {
            resolve(response)
          })
          .catch((err) => {
            handleError(err)
            reject(err)
          })
      })
    }

  },
})